﻿// Dùng tại frmDoanhThu.sbtnIn_Click(), frmTongHopDT

using System;
using System.Data.SqlClient;

using System.Data;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraReports.UI;
using prjKQXS_DLL;
using prjKQXS_DLL.Doanh_thu;

namespace prjKQXS_Report
{
    public partial class rptMauHH01x : DevExpress.XtraReports.UI.XtraReport
    {
        //Cài đặt event
        public delegate void OnLoadCompleteHandler();
        public event OnLoadCompleteHandler OnLoadComplete;

        dsReports dsReports1 = new dsReports();
        string slblSumHT2 = "";

        public rptMauHH01x()
        {
            InitializeComponent();
        }

        private void rptMauHH01x_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            reDesign();
            fillHeader();

            Report_DAL report_dal = new Report_DAL();
            dsReports1 = report_dal.reportDungChung(dsReports1);

            clsDoanhThu cls = new clsDoanhThu();
            cls.OnAction += new clsDoanhThu.OnActionHandler(cls_OnAction);
            cls.OnError += new clsDoanhThu.OnErrorHandler(cls_OnError);

            dsReports1.Tables.Remove("rptHoaHong");
            dsReports1.Tables.Add(cls.loadData_rptMauHH01x(Convert.ToChar(pMaDV.Value), (int)pDMCanBoID.Value,
                (DateTime)pTuNgay.Value, (DateTime)pDenNgay.Value, ref slblSumHT2, (bool)pDaiLyThiTran.Value));
            this.DataSource = dsReports1;
            this.DataMember = "rptHoaHong";
        }

        private void rptMauHH01x_AfterPrint(object sender, EventArgs e)
        {
            OnLoadComplete();
        }

        #region cls event --------------------------------------------
        private void cls_OnAction(SqlException e)
        {
            cls_OnError(e);
        }

        private void cls_OnError(Exception e)
        {
            XtraMessageBox.Show("Một lỗi chưa được xử lý đã xảy ra. Chi tiết lỗi: \n" + e.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            this.ClosePreview();
        }
        #endregion ---------------------------------------------------

        private void reDesign()
        {
            GroupHeader1.Visible = !((string)pFormName.Value == "frmTongHopDT" || (string)pFormName.Value == "frmDoanhThu");

            if ((string)pFormName.Value == "frmTongHopDT"
                || (((string)pFormName.Value == "frmDoanhThu") && (int)pDMCanBoID.Value != -1))
            {
                GroupHeader1.GroupFields["Ma_CanBo"].FieldName = "MaDaiLy";
                Detail.Visible = false;

                xrtblFooter2.LocationF = xrtblFooter1.LocationF;
                GroupFooter1.Controls.Remove(xrtblFooter1);
                GroupFooter1.HeightF = xrtblFooter2.HeightF;
            }
            else if ((string)pFormName.Value == "frmDoanhThu")
            {
                if (Convert.ToChar(pMaDV.Value) == '%')
                {
                    GroupFooter1.Controls.Remove(xrtblFooter2);
                    GroupFooter1.HeightF = xrtblFooter1.HeightF;
                }
                else
                {
                    GroupFooter1.Controls.Remove(xrtblFooter2);
                    GroupFooter1.HeightF = xrtblFooter1.HeightF;
                }
            }

            if ((string)pCap.Value == "CTY" || (string)pCap.Value == "QH")
            {
                if ((bool)pDaiLyThiTran.Value)
                {
                    if (!(bool)pKhongDuDoanhThu.Value)
                    {
                        lblMau.Text = (string)pCap.Value == "CTY" ? "Mẫu HH-01A-TT1" : "Mẫu HH-01B-TT1";
                    }
                    else lblMau.Text = (string)pCap.Value == "CTY" ? "Mẫu HH-01A-TT2" : "Mẫu HH-01B-TT2";
                }
                else lblMau.Text = (string)pCap.Value == "CTY" ? "Mẫu HH-01A" : "Mẫu HH-01B";
            }
            else lblMau.Text = "Mẫu HH-01C";
           
        }

        private void fillHeader()
        {
            if ((int)pDMCanBoID.Value == -1 && !(bool)pDaiLyThiTran.Value) xrlblKyTen1.Visible = false;

            if ((int)pDMCanBoID.Value != -1)lblCanBo.Text = "Người quản lý: " + pTenCB.Value;
            else lblCanBo.Visible = false;

            if (((DateTime)pTuNgay.Value).Date.CompareTo(((DateTime)pDenNgay.Value).Date) == 0)
                lblNgayThang.Text = "Ngày " + ((DateTime)pTuNgay.Value).ToString("dd/MM/yyyy");
            else
                lblNgayThang.Text = "Từ ngày " + ((DateTime)pTuNgay.Value).ToString("dd/MM/yyyy")
                    + " đến ngày " + ((DateTime)pDenNgay.Value).ToString("dd/MM/yyyy");
        }

        int stt = 0;
        private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            STT.Text = (++stt).ToString();
        }

        int iSTT = 0;
        private void GroupHeader1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            xrtcSTT.Text = (++iSTT).ToString();
        }

        int stt5 = 0;
        private void GroupFooter1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            xrtcSTT5.Text = (++stt5).ToString();

            if ((bool)pKhongDuDoanhThu.Value)
            {
                decimal result = (decimal)xrtcTongDT1.Summary.GetResult();
                int SoNgay = ((DateTime)pDenNgay.Value - (DateTime)pTuNgay.Value).Days + 1;
                e.Cancel = !((result / SoNgay) < 300);
                stt5 = e.Cancel ? --stt5 : stt5;
            }
        }


        #region Tính %HT ================================================================
        //Dùng chung cho xrtcHT1, xrtcHT2, lblHT

        decimal dt = 0;
        decimal kh = 0;
        private void xrtcHT_SummaryReset(object sender, EventArgs e)
        {
            dt = 0;
            kh = 0;
        }

        private void xrtcHT_SummaryRowChanged(object sender, EventArgs e)
        {
            dt += GetCurrentColumnValue("TongDT") == DBNull.Value ? 0 : Convert.ToInt32(GetCurrentColumnValue("TongDT"));
            kh += GetCurrentColumnValue("DoanhThu") == DBNull.Value ? 0 : Convert.ToInt32(GetCurrentColumnValue("DoanhThu"));
        }

        private void xrtcHT_SummaryGetResult(object sender, SummaryGetResultEventArgs e)
        {
            if (dt / (kh == 0 ? 1 : kh) * 100 != 0) e.Result = dt / (kh == 0 ? 1 : kh) * 100;
            e.Handled = true;
        }
        #endregion Tính %HT =============================================================
        //#region Đoạn này để không in ra các tổng HH bằng 0 -------------------------------------
        //decimal HoaHong, TThong, Boc, Loto234, Capso  ;
        //private void SummaryGetResult(object sender, SummaryGetResultEventArgs e)
        //{
        //    string dataMember = ((XRTableCell) sender).DataBindings[0].DataMember;
        //    int dot = dataMember.IndexOf('.');
        //    dataMember = dataMember.Substring(dot + 1, dataMember.Length - dot - 1);
        //    decimal value = 0;
        //    if (dataMember == "HoaHong") value = HoaHong;
        //    else if (dataMember == "TThong") value = TThong;
        //    else if (dataMember == "Boc") value = Boc;
        //    else if (dataMember == "Loto234") value = Loto234;
        //    else if (dataMember == "Capso") value = Capso;
        //    e.Handled = value == 0;
        //}

        //private void SummaryReset(object sender, EventArgs e)
        //{
        //    HoaHong = 0;
        //    TThong = 0;
        //    Boc = 0;
        //    Loto234 = 0;
        //    Capso = 0;
        //}

        //private void SummaryRowChanged(object sender, EventArgs e)
        //{
        //    HoaHong += clsCommon.getDecimal(GetCurrentColumnValue("HoaHong"));
        //    TThong += clsCommon.getDecimal(GetCurrentColumnValue("TThong"));
        //    Boc += clsCommon.getDecimal(GetCurrentColumnValue("Boc"));
        //    Loto234 += clsCommon.getDecimal(GetCurrentColumnValue("Loto234"));
        //    Capso += clsCommon.getDecimal(GetCurrentColumnValue("Capso"));
        //}

        
        //#endregion -----------------------------------------------------------------------------

        private void lblSumHT2_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            lblSumHT2.Text = slblSumHT2;
        }

        private void xrTableCell3_SummaryGetResult(object sender, SummaryGetResultEventArgs e)
        {

        }
        //#region Đoạn này để không in ra các tổng HH bằng 0 -------------------------------------

        //decimal TongDT;
        //private void xrtcTongDT1_SummaryGetResult(object sender, SummaryGetResultEventArgs e)
        //{

        //}

        //private void xrtcTongDT1_SummaryReset(object sender, EventArgs e)
        //{

        //}

        //private void xrtcTongDT1_SummaryRowChanged(object sender, EventArgs e)
        //{

        //}

        //#endregion -----------------------------------------------------------------------------

    }
}
